package org.mine.design.builder.controller;

import lombok.extern.slf4j.Slf4j;
import org.mine.design.builder.controller.param.RequestParam;
import org.mine.design.builder.core.BaseEarningsBuilder;
import org.mine.design.builder.core.EarningsBuilder;
import org.mine.design.builder.core.EarningsResult;
import org.mine.design.builder.enums.DriverTypeEnum;
import org.mine.design.builder.enums.EarningsTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 构建者模式
 */
@Slf4j
@RestController
public class BuilderController {


    @Autowired
    private List<EarningsBuilder> earningsBuilders;


    /**
     * 模拟打车软件，计算司机完成一单后收益明细信息
     */
    @RequestMapping("/builder/test")
    public void test(RequestParam param) {
        DriverTypeEnum driverType = param.getDriverType();
        Optional<EarningsBuilder> earningsBuilder = earningsBuilders.stream()
                .filter(service -> service.getDriverType() == driverType)
                .findFirst();
        earningsBuilder.ifPresent(
                service -> {
                    //收入金额
                    Integer incomeAmount = service.buildIncome(param).stream()
                            .map(EarningsResult::getAmount)
                            .reduce(Integer::sum)
                            .orElse(0);
                    //扣减金额
                    Integer deductionAmount = service.buildDeduction(param).stream()
                            .map(EarningsResult::getAmount)
                            .reduce(Integer::sum)
                            .orElse(0);

                    log.info("收入 = {},扣减 = {}",incomeAmount,deductionAmount);
                }
        );
    }
}
